{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "!mv PaddleDetection-2.1.0 PaddleDetection "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "!unzip -o data/data85839/RBC.zip -d PaddleDetection/dataset/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "!mv PaddleDetection/dataset/RBC PaddleDetection/dataset/test_det"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os \r\n",
    "import random\r\n",
    "\r\n",
    "# 类别数量 \r\n",
    "file_saved = []   # 保存数据\r\n",
    "\r\n",
    "random.seed(2021)\r\n",
    "\r\n",
    "# voc数据路径问题\r\n",
    "# 根目录信息，子目录信息，files_img--该文件夹下的文件名称\r\n",
    "for _, _, files_img in os.walk('PaddleDetection/dataset/test_det/JPEGImages'):\r\n",
    "    random.shuffle(files_img)\r\n",
    "\r\n",
    "    for _, _, files_xml in os.walk('PaddleDetection/dataset/test_det/Annotations'):\r\n",
    "        # indexs = 0\r\n",
    "        # 1.jpg\r\n",
    "        # 1.xml\r\n",
    "        for i in range(len(files_img)):  # 遍历图片文件--一张一张的\r\n",
    "            for j in range(len(files_xml)):\r\n",
    "                # 匹配，与图片前缀名称一致的xml文件\r\n",
    "                # 前缀是否一致\r\n",
    "                if files_img[i][:-4] == files_xml[j][:-4]:\r\n",
    "                    # 图片的相对路径 + 空格 + 标注文件的相对路径 + '\\n'\r\n",
    "                    # jpeg, img -- join -> jpeg/img\r\n",
    "                    # JPEGImages/files_img[i]\r\n",
    "                    file_maked = os.path.join('JPEGImages', files_img[i]) + ' ' + os.path.join('Annotations', files_xml[j])  + '\\n'\r\n",
    "                    \r\n",
    "                    file_saved.append(file_maked)          # 每一个类别放在对应的缓存空间中\r\n",
    "                    break\r\n",
    "\r\n",
    "# example: 图片的相对路径 + 空格 + 标注文件的相对路径 + '\\n'\r\n",
    "\r\n",
    "# 训练集的划分\r\n",
    "# 训练集占80%的数据\r\n",
    "# 验证集/评估数据集：1-80% = 20%\r\n",
    "Train_percent = 0.8\r\n",
    "\r\n",
    "# train.txt保存\r\n",
    "with open('PaddleDetection/dataset/test_det/train.txt', 'w') as f:\r\n",
    "    # int(Train_percent * len(file_saved))\r\n",
    "    # final_index = int(len(file_saved)*Train_percent) - 1\r\n",
    "    f.writelines(file_saved[:int(len(file_saved)*Train_percent)])  # 写入多行数据\r\n",
    "    print('train.txt Has Writed {0} records!'.format(len(file_saved[:int(len(file_saved)*Train_percent)])))\r\n",
    "    \r\n",
    "# eval.txt保存\r\n",
    "with open('PaddleDetection/dataset/test_det/eval.txt', 'w') as f:\r\n",
    "    # final_index + 1 == int(len(file_saved)*Train_percent)\r\n",
    "    f.writelines(file_saved[int(len(file_saved)*Train_percent):])\r\n",
    "    print('eval.txt Has Writed {0} records!'.format(len(file_saved[int(len(file_saved)*Train_percent):])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%cd /home/aistudio/PaddleDetection/\r\n",
    "!pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%cd /home/aistudio/PaddleDetection/\r\n",
    "!python tools/train.py -c configs/ppyolo/ppyolo_r18vd_coco.yml"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.1.0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
